// Server API makes it possible to hook into various parts of Gridsome
// on server-side and add custom data to the GraphQL data layer.
// Learn more: https://gridsome.org/docs/server-api/

// Changes here require a server restart.
// To restart press CTRL + C in terminal and run `gridsome develop`
const axios = require('axios')
const Cookies=require( 'js-cookie')
module.exports = function (api) {
  const githubUsername="ligoushi";
  const baseURL= "https://api.github.com";
  api.loadSource(async ({ addCollection }) => {
    // Use the Data Store API here: https://gridsome.org/docs/data-store-api/
    const collection = addCollection('Post')

    const { data } = await axios.get('https://jsonplaceholder.typicode.com/posts')

    for (const item of data) {
      collection.addNode({
        id: item.id,
        title: item.title,
        content: item.body
      })
    }
  })
  api.loadSource(({ addCollection }) => {
    const collection = addCollection("config")
    const config = {
      "githubUsername": githubUsername,
      "blogTitle": githubUsername,
      "blogDescribe": "欢迎来到"+githubUsername+"的个人博客。",
      "htmlTitle": githubUsername+"的博客",
      "fontColor": "#ffffff",
      "useBackgroundImage": false,
      "backgroundColorLeft": "#2690F9",
      "backgroundColorRight": "#FC2D2D",
      "audioAutoPlay":false,
      "audioUrl": "http://sc1.111ttt.cn:8282/2018/1/03m/13/396131232171.m4a?tflag=1519095601&pin=6cd414115fdb9a950d827487b16b5f97#.mp3",
      "mini": false,
      "webSites": [
        { "name": "v2ex", "url": "https://www.v2ex.com/member/laziji" }
      ]
    }
    collection.addNode(config)
  })
  api.loadSource(async ({ addCollection }) => {
    const collection = addCollection("user")
    let url="/users/ligoushi?access_token=8610def01970c7d31fbde40d199ec929b2d9749b"
    const { data } = await axios.get(baseURL+url)
    console.log(data);
    collection.addNode(data);
  })
  api.createPages(({ createPage }) => {
    // Use the Pages API here: https://gridsome.org/docs/pages-api/
  })
}
